package vistas;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JTable;
import javax.swing.JPopupMenu;
import java.awt.Component;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JMenuItem;

import clases.Registropagos_class;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.ResultSet;
import java.util.ArrayList;
import javax.swing.ImageIcon;
import java.awt.event.KeyEvent;
import java.awt.Cursor;
import javax.swing.JScrollPane;
import java.awt.Toolkit;

public class Pagos_buscarform extends JInternalFrame {

	private final JPanel contentPanel = new JPanel();
	private JTextField tbuscar;
	private JTable tabla_pagos;
	private int filas;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			Pagos_buscarform dialog = new Pagos_buscarform();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public Pagos_buscarform() {
		setFrameIcon(new ImageIcon(Asignarhorarios_buscarform.class.getResource("/icons/extra.png")));
		setClosable(true);
		setIconifiable(true);
		setTitle("Buscar Pagos");
		setBounds(100, 100, 565, 372);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		{
			JLabel lblPagos = new JLabel("Pagos");
			lblPagos.setFont(new Font("Tahoma", Font.PLAIN, 13));
			lblPagos.setBounds(10, 14, 41, 16);
			contentPanel.add(lblPagos);
		}
		{
			tbuscar = new JTextField();
			tbuscar.setFont(new Font("Tahoma", Font.PLAIN, 13));
			tbuscar.setColumns(10);
			tbuscar.setBounds(61, 11, 351, 22);
			contentPanel.add(tbuscar);
		}
		{
			JButton button = new JButton("Buscar");
			button.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
			button.setContentAreaFilled(false);
			button.setMnemonic(KeyEvent.VK_ENTER);
			button.setIcon(new ImageIcon(Pagos_buscarform.class.getResource("/icons/buscar.png")));
			button.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					metodoBuscar();
				}
			});
			button.setBounds(422, 12, 117, 23);
			contentPanel.add(button);
		}
		{
			JScrollPane scrollPane = new JScrollPane();
			scrollPane.setBounds(10, 41, 529, 282);
			contentPanel.add(scrollPane);
			{
				tabla_pagos = new JTable();
				scrollPane.setViewportView(tabla_pagos);
				tabla_pagos.setModel(new DefaultTableModel(
					new Object[][] {
					},
					new String[] {
						"codigoregistropago", "Nombre", "Fecha", "Valor"
					}
				));
				tabla_pagos.getColumnModel().getColumn(0).setPreferredWidth(15);
				tabla_pagos.getColumnModel().getColumn(0).setMinWidth(0);
				tabla_pagos.getColumnModel().getColumn(0).setMaxWidth(0);
				{
					JPopupMenu popupMenu = new JPopupMenu();
					addPopup(tabla_pagos, popupMenu);
					{
						JMenuItem mntmMostrar = new JMenuItem("Mostrar");
						mntmMostrar.setIcon(new ImageIcon(Pagos_buscarform.class.getResource("/icons/modificar_pq.png")));
						mntmMostrar.addActionListener(new ActionListener() {
							public void actionPerformed(ActionEvent arg0) {
								if(tabla_pagos.getSelectedRow()>-1)
								{
									Pagos_ingresarform modificar = new Pagos_ingresarform(tabla_pagos.getValueAt(tabla_pagos.getSelectedRow(), 0).toString());
									Principal.contenedor.add(modificar);
									modificar.setVisible(true);
								}
							}
						});
						popupMenu.add(mntmMostrar);
					}
				}
			}
		}
		metodoBuscar();
	}

	private static void addPopup(Component component, final JPopupMenu popup) {
		component.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}
			public void mouseReleased(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}
			private void showMenu(MouseEvent e) {
				popup.show(e.getComponent(), e.getX(), e.getY());
			}
		});
	}
	
	private void metodoBuscar()
	{

		try
		{

			DefaultTableModel mpagos = (DefaultTableModel)tabla_pagos.getModel();
			while(mpagos.getRowCount()>0) mpagos.removeRow(0);
			
			filas=0;
			
			String query = "SELECT  (ficha.nombres+ ' ' +ficha.primerapellido+ ' '+  ficha.segundoapellido) AS nombres,  CONVERT(varchar,registropagos.fecha,110) AS fecha,  registropagos.valor" +
					", registropagos.codigoregistropago AS codigoregistropago"+
						"  FROM dbo.registropagos " +
						" INNER JOIN dbo.registroasistencias  ON registropagos.codigoasistencia = registroasistencias.codigoregistro"+
						" INNER JOIN dbo.empleadohorarios  ON registroasistencias.codigoempleadohorario = empleadohorarios.codigoempleadohorario "+
						" INNER JOIN dbo.ficha   ON empleadohorarios.codigoficha = ficha.codigonit " +
						" WHERE nombres LIKE ?";
			ArrayList<String> parametros = new ArrayList<String>();
			parametros.add("%"+tbuscar.getText()+"%");
			
			ResultSet resultado = new Registropagos_class().mostrar(query, parametros);
			while(resultado.next())
			{
				mpagos.addRow(new Object[filas]);
				mpagos.setValueAt(resultado.getString("codigoregistropago"), filas, 0);
				mpagos.setValueAt(resultado.getString("nombres"), filas, 1);
				mpagos.setValueAt(resultado.getString("fecha"), filas, 2);
				mpagos.setValueAt(String.format("%.2f", (float)Math.round((resultado.getDouble("valor"))*100)/100), filas, 3);
				filas++;
			}
			resultado.close();
			
			
		}catch(Exception ex)
		{
			JOptionPane.showMessageDialog(null, ex.toString());
		}
	
	}
}
